package com.hncboy.chatgpt.front.api.utli;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;



/**
 * 二维码生成
 */
public class QRCodeGeneratorUtil {

    //二维码图片路径
    public static final String QR_CODE_IMAGE_PATH = "D:/tmp/images/";
//    public static final String QR_CODE_IMAGE_PATH = "/www/wwwroot/chat.apiidv.xyz/images/";

    /**
     * 生成二维码
     *
     * @param text     文本或请求链接
     * @param width    二维码图片宽度
     * @param height   二维码图片高度
     * @param filePath 二维码存放位置(包括文件名)
     * @throws WriterException
     * @throws IOException
     */
    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }
}

